package com.hotau.ui;

import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.graphics.Rect;
import android.util.AttributeSet;
import android.util.Log;
import android.view.View;

import com.hotau.HotauActivity;
import com.hotau.R;

/**
 * 
 * Class Name: PageIndicatorView.java Function: extends {@link View},show the
 * dot let user know the page position at the gallery
 * 
 * Modifications:
 * 
 * @author DateTime 2011-11-1 下午07:48:36
 * @version 1.0
 */
public class PageIndicatorView extends View {
	private int mCurrentPage = -1;
	private int mTotalPage = 0;

	public static int indicatorWidth = 10;
	public static int indicatorHeight = 2;

	public PageIndicatorView(Context context) {
		super(context);
	}

	public PageIndicatorView(Context context, AttributeSet attrs) {
		super(context, attrs);
	}

	public void setTotalPage(int nPageNum) {
		mTotalPage = nPageNum;
		if (mCurrentPage >= mTotalPage)
			mCurrentPage = mTotalPage - 1;
	}

	public int getCurrentPage() {
		return mCurrentPage;
	}

	public void setCurrentPage(int nPageIndex) {
		if (nPageIndex < 0 || nPageIndex >= mTotalPage)
			return;
		if (mCurrentPage != nPageIndex) {
			mCurrentPage = nPageIndex;
			this.invalidate();
		}
	}

	@Override
	protected void onDraw(Canvas canvas) {
		super.onDraw(canvas);

		Paint paint = new Paint(Paint.ANTI_ALIAS_FLAG
				| Paint.FILTER_BITMAP_FLAG);
		paint.setStyle(Paint.Style.FILL);
		paint.setColor(Color.BLACK);
		Rect r = new Rect();
		this.getDrawingRect(r);
		int space = 5;
		Bitmap bmp = BitmapFactory.decodeResource(getResources(),
				R.drawable.dot_unselected);
		int iconWidth = bmp.getWidth();
		int iconHeight = bmp.getHeight();

		Log.d("mytag", "原始iconWidth=" + iconWidth + " iconHeight=" + iconHeight);
		iconWidth = (int) (indicatorWidth * HotauActivity.galleryImageRate);
		iconHeight = (int) (indicatorHeight * HotauActivity.galleryImageRate);
		Log.d("mytag", "修改后iconWidth=" + iconWidth + " iconHeight=" + iconHeight);

		int x = (r.width() - (iconWidth * mTotalPage + space * (mTotalPage - 1))) / 2;
		int y = (r.height() - iconHeight) / 2;
		// int y = 0;
		Log.d("mytag", "x=" + x + " y=" + y);
		for (int i = 0; i < mTotalPage; i++) {
			int resid = R.drawable.dot_unselected;
			if (i == mCurrentPage) {
				resid = R.drawable.dot_selected;
			}
			Bitmap bmp1 = BitmapFactory.decodeResource(getResources(), resid);
			Rect r1 = new Rect();
			r1.left = x;
			r1.top = 0;
			r1.right = x + iconWidth;
			r1.bottom = y + iconHeight;

			Log.d("mytag", "r1.left=" + r1.left + " r1.top=" + r1.top + " r1.right=" + r1.right + " r1.bottom=" + r1.bottom);
			
			canvas.drawBitmap(bmp1, null, r1, paint);
			x += iconWidth + space;
		}
	}
}